package com.axend.aerosense.dev.activity;

import android.os.Bundle;

import androidx.annotation.Nullable;
import androidx.databinding.ViewDataBinding;
import androidx.fragment.app.Fragment;

import com.axend.aerosense.base.activity.MvvmBaseActivity;
import com.axend.aerosense.base.utils.BleUtils;
import com.axend.aerosense.base.utils.WifiUtils;
import com.axend.aerosense.base.viewmodel.BaseViewModel;
import com.axend.aerosense.common.base.IHostLayout;
import com.axend.aerosense.common.bean.RadarType;
import com.axend.aerosense.common.connect.impl.IDeviceConnect;
import com.axend.aerosense.common.connect.impl.IScanCallback;
import com.axend.aerosense.common.connect.impl.IScanResult;
import com.axend.aerosense.common.connect.impl.IScanStateCallback;
import com.axend.aerosense.common.connect.ble.BleClient;
import com.axend.aerosense.common.connect.tcp.NettyClient;
import com.axend.aerosense.common.ui.LoadingDialog;
import com.axend.aerosense.dev.impl.IWaitDialog;
import com.axend.aerosense.common.connect.DeviceConnectManager;
import com.axend.aerosense.common.connect.tcp.IDeviceConnectCallback;

import java.util.List;

/**
 * 在此Activity上统一管理wifi的连接，获取wifi列表
 * Date: 2021/3/9 11:30
 */
public abstract class DevBaseWifiActivity<V extends ViewDataBinding, VM extends BaseViewModel>
        extends MvvmBaseActivity<V, VM> implements IDeviceConnectCallback, IHostLayout, IWaitDialog, IScanStateCallback {

    private DeviceConnectManager deviceConnectManager;
    private LoadingDialog loadingDialog;

    @Override
    protected void onCreate(@Nullable Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        if (null == deviceConnectManager) {
            deviceConnectManager = new DeviceConnectManager(this);
            deviceConnectManager.setDeviceConnectCallback(this);
            deviceConnectManager.setScanCallback(this);
        }
    }

    /**
     * 初始化与连接模式设置
     * @param radarType
     */
    public void setDeviceType(RadarType radarType){
        IDeviceConnect deviceConnect;
        if (radarType == RadarType.ASSURE) {
            deviceConnect = NettyClient.getInstance();
        } else {
            deviceConnect = BleClient.getInstance();
        }
        deviceConnectManager.setDeviceConnect(deviceConnect);

//        if (null == loadingDialog){
//            loadingDialog = new LoadingDialog.Builder(this)
//                    .setMessage(getString(R.string.dev_please_wait))
//                    .setCancelable(true)
//                    .setCancelOutside(false)
//                    .setShowMessage(true)
//                    .create();
//        }
    }

    public DeviceConnectManager getDeviceConnectManager() {
        return deviceConnectManager;
    }

    /** 是否启用WIFI */
    public boolean isEnableWifi() {
        return WifiUtils.getInstance().isEnable();
    }

    /** 打开wifi */
    public void openWifi(){
        if (deviceConnectManager.isAndroid10()){
            //Android 10及以上版本让用户手动打开
            if (!isEnableWifi()){
                //showTipDialog(DeviceConnectManager.CONNECT_MODE_WIFI);
            }
        }else {
            //可用代码打开
            WifiUtils.getInstance().openWifi();
        }
    }

    public boolean isEnableBluetooth(){
        return BleUtils.getInstance().isEnable();
    }

    /** 打开蓝牙 */
    public void openBluetooth(){
        if (!isEnableBluetooth()){
            //showTipDialog(DeviceConnectManager.CONNECT_MODE_BLE);
        }
    }

    /** 显示等待框 */
    @Override
    public void showWaitDialog(){
        if (null != loadingDialog){
            loadingDialog.show();
        }
    }

    /** 隐藏等待框 */
    @Override
    public void hideWaitDialog(){
        if (null != loadingDialog){
            loadingDialog.dismiss();
        }
    }

    @Override
    public void isConnectSuccess(boolean isSuccess) {
        runOnUiThread(()-> {
            Fragment currFrag = (Fragment) getSupportFragmentManager().findFragmentById(getContentId());
            if (currFrag instanceof IDeviceConnectCallback){
                ((IDeviceConnectCallback)currFrag).isConnectSuccess(isSuccess);
            }
        });
    }

    @Override
    public void deviceData(Object msg) {
        runOnUiThread(()-> {
            Fragment currFrag = (Fragment) getSupportFragmentManager().findFragmentById(getContentId());
            if (currFrag instanceof IDeviceConnectCallback){
                ((IDeviceConnectCallback)currFrag).deviceData(msg);
            }
        });
    }

    @Override
    public void scanResult(List<IScanResult> list) {
        runOnUiThread(()->{
            Fragment currFrag = (Fragment) getSupportFragmentManager().findFragmentById(getContentId());
            if (currFrag instanceof IScanCallback){
                ((IScanCallback)currFrag).scanResult(list);
            }
        });
    }

    @Override
    public void scanState(int state) {
        runOnUiThread(()->{
            Fragment currFrag = (Fragment) getSupportFragmentManager().findFragmentById(getContentId());
            if (currFrag instanceof IScanStateCallback){
                ((IScanStateCallback)currFrag).scanState(state);
            }
        });
    }

    /**
     * 关闭与设备的连接
     */
    public void closeDeviceConnect(){
        if (deviceConnectManager == null) return;
        deviceConnectManager.close();
    }

    @Override
    protected void onDestroy() {
        super.onDestroy();
        //Activity销毁时释放资源
        if (deviceConnectManager != null){
            deviceConnectManager.close();
            deviceConnectManager.clear();
        }
        if (null != loadingDialog){
            loadingDialog.dismiss();
            loadingDialog = null;
        }
        deviceConnectManager = null;
    }
}
